﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace Testharness
{
    public partial class UniqueSymbolsListForm : Form
    {
        public UniqueSymbolsListForm()
        {
            InitializeComponent();
        }

        private void _SymbolsListBox_DragDrop(object sender, DragEventArgs e)
        {
            string[] files = (string[])e.Data.GetData(DataFormats.FileDrop);

            if (files != null && files.Length > 0)
            {
                string filename = files[0];

                //make sure file hasnt already been added
                if (!_FilesListBox.Items.Contains(filename))
                {
                    _FilesListBox.Items.Add(filename);

                    using (StreamReader sr = new StreamReader(filename))
                    {
                        //make sure we havent reached the end
                        while (sr.Peek() >= 0)
                        {
                            //read the symbol line
                            string symbol = sr.ReadLine();

                            //does the list box contain the symbol
                            if (!_SymbolsListBox.Items.Contains(symbol))
                            {
                                try
                                {
                                    _SymbolsListBox.Items.Add(symbol);
                                }
                                catch (Exception exception)
                                {
                                    MessageBox.Show(exception.ToString());
                                }
                            }
                        }
                    }

                    //sort the symbols
                    List<string> symbolsList = new List<string>();

                    foreach (string symbol in _SymbolsListBox.Items)
                    {
                        symbolsList.Add(symbol);
                    }

                    symbolsList.Sort();

                    _SymbolsListBox.Items.Clear();

                    foreach (string symbol in symbolsList)
                    {
                        _SymbolsListBox.Items.Add(symbol);
                    }

                    _SymbolsCountLabel.Text = _SymbolsCountLabel.Text =
                        string.Format("{0} symbols", _SymbolsListBox.Items.Count);
                }
                else
                {
                    MessageBox.Show("File already added");
                }
            }
        }

        private void _SymbolsListBox_DragEnter(object sender, DragEventArgs e)
        {
            if (e.Data.GetDataPresent(DataFormats.FileDrop, false) == true)
                // allow them to continue
                // (without this, the cursor stays a "NO" symbol
                e.Effect = DragDropEffects.All;

        }

        private void _ClearListButton_Click(object sender, EventArgs e)
        {
            _SymbolsListBox.Items.Clear();

            _FilesListBox.Items.Clear();

            _SymbolsCountLabel.Text = "";
        }

        private void _SaveFileButton_Click(object sender, EventArgs e)
        {
            if (_SaveFileDialog.ShowDialog() == DialogResult.OK)
            {
                string location = _SaveFileDialog.FileName;

                StringBuilder stringBuilder = new StringBuilder();

                foreach (string symbol in _SymbolsListBox.Items)
                {
                    stringBuilder.AppendLine(symbol);
                }

                //write out the file symbols
                File.WriteAllText(location, stringBuilder.ToString());
            }
        }
    }
}
